package com.osell.selection.ui.product;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.osell.selection.data.network.entity.ProductDetail;
import com.osell.selection.ui.base.BaseFragment;
import com.osell.selection.ui.base.SimpleFragmentPagerAdapter;
import com.osell.selection.ui.common.WebViewFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ProductFootFragment extends BaseFragment {

    @BindView(R.id.tab_product_foot)
    TabLayout tab_product_foot;
    @BindView(R.id.vp_product_foot)
    ViewPager vp_product_foot;

    private Unbinder mUnBinder;

    private ProductDetail mProductDetail;

    public static ProductFootFragment newInstance(ProductDetail productDetail) {
        ProductFootFragment fragment = new ProductFootFragment();
        Bundle args = new Bundle();
        args.putSerializable("product_detail", productDetail);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            mProductDetail = (ProductDetail) args.getSerializable("product_detail");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.product_detail_foot, container, false);
        mUnBinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        vp_product_foot.setAdapter(new SimpleFragmentPagerAdapter(getChildFragmentManager())
                .addFragment(WebViewFragment.newInstance(XLGConstants.WEB_URL + "p/" + mProductDetail.number + ".html?viewtype=1"), "Description")
                .addFragment(WebViewFragment.newInstance(XLGConstants.WEB_URL + "p/" + mProductDetail.number + ".html?viewtype=3"), "Comment"));
        tab_product_foot.setupWithViewPager(vp_product_foot);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
    }
}
